import matplotlib.pyplot as plt
import numpy as np

# 设置中文字体支持（如果系统不支持中文可省略）
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号

# 创建数据
methods = ['Distributed DTFL', 'Centralized PMS']
accuracy_rates = [87.6, 0]  # 准确率保持率数据

# 创建图表
plt.figure(figsize=(8, 5))
ax = plt.subplot()

# 设置柱状图颜色
colors = ['#4C72B0', '#C44E52']

# 绘制柱状图
bars = ax.bar(methods, accuracy_rates, color=colors, width=0.6, edgecolor='black', linewidth=1.2)

# 添加数据标签
for i, bar in enumerate(bars):
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2., height+2, 
            f'{height}%' if height > 0 else 'Failure', 
            ha='center', va='bottom', fontsize=12, fontweight='bold')

# 添加基准线
ax.axhline(y=0, color='black', linewidth=0.8)

# 设置标题和标签
# plt.title('Figure 4.4: Accuracy Retention Rate During Edge Server Failure', fontsize=14, pad=15)
plt.ylabel('Accuracy Retention Rate (%)', fontsize=17, labelpad=10)
plt.xticks(fontsize=17)
plt.ylim(0, 100)  # 设置y轴范围

# # 添加网格线
# ax.grid(axis='y', linestyle='--', alpha=0.7)

# 添加图例说明
# plt.figtext(0.5, 0.01, 
#             'Note: Centralized PMS fails completely during server failure while Distributed DTFL maintains 87.6% accuracy', 
#             ha='center', fontsize=10, style='italic')

# 优化布局
plt.tight_layout()
plt.subplots_adjust(bottom=0.15)  # 为底部说明留出空间

# 保存图像（可选）
# plt.savefig('figure_4_4.png', dpi=300, bbox_inches='tight')

# 显示图表
plt.savefig('Fig4.png', dpi=600)
plt.show()
